package com.kdtech.analyse.outside;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;

import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;

/**
 * @name 中国数字时代
 * @link http://chinadigitaltimes.net/chinese/
 * @author dhy
 */
public class ChinadigitaltimesOutsideAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://chinadigitaltimes.net/chinese/[0-9]*/[0-9]*/.*/"
	};


	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);

		title=doc.select("h1.title").text();
		if(StringUtils.isNotBlank(title))
			title = StringUtils.substringAfter(title, "|");
		if(StringUtils.isBlank(title))
			title=doc.select("title").text();
		if(StringUtils.isNotBlank(title))
			if(title.indexOf("-")!=-1)
				title = StringUtils.substringBefore(title, "-");

		date=DateUtils.matchDate(doc.select("div.meta").text());

		doc.select("div.sharedaddy").remove();
		content =HtmlCleaner.getContentHtml(url, doc.select("div.entry"));

		author=JSoupUtils.matchAuthor(doc, "来源：");
		news.setTitle(StringUtils.trimSpace(title));
		news.setAuthor(author);
		news.setContent(content);
		news.setDate(date);

		return news;

	}

	

	

	
}

